import clientFunc from "./client.func"

export default class DerivedSensorReading {
  baseReading: any
  distanceCache: number
  distanceNeedsUpdate: boolean
  constructor(t) {
    this.baseReading = t
    this.distanceCache = 1 / 0
    this.distanceNeedsUpdate = !0
  }
  get facade() {
    const t = this
    return {
      get inRange() {
        return t.baseReading.inRange
      },
      get inView() {
        return t.baseReading.inView
      },
      get distanceSquared() {
        return t.baseReading.distanceSquared
      },
      get direction() {
        return t.baseReading.direction
      },
      get distance() {
        return t.distance
      }
    }
  }
  update(t) {
    ;(this.baseReading.inRange = t.inRange), (this.baseReading.inView = t.inView), this.updateDistance(t.distanceSquared), this.updateDirection(t.direction)
  }
  updateDirection(t) {
    clientFunc.at(this.baseReading.direction, t)
  }
  updateDistance(t) {
    t && ((this.distanceNeedsUpdate = !0), (this.baseReading.distanceSquared = t))
  }
  get distance() {
    return this.distanceNeedsUpdate && ((this.distanceNeedsUpdate = !1), (this.distanceCache = Math.sqrt(this.baseReading.distanceSquared))), this.distanceCache
  }
}
